package com.zhengshang.api.controller.goodsshop;

import com.alibaba.fastjson.util.TypeUtils;
import com.zhengshang.api.service.IAppPayRecordService;
import com.zhengshang.common.constant.app.AppConstant;
import com.zhengshang.common.core.domain.AjaxResult;
import com.zhengshang.common.core.domain.entity.SysUser;
import com.zhengshang.common.core.domain.model.LoginAppUser;
import com.zhengshang.common.utils.StringUtils;
import com.zhengshang.security.service.TokenService;
import com.zhengshang.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "订单接口")
@Controller
@RequestMapping("/order")
public class AppOrderController {

    @Autowired
    private IAppPayRecordService appPayRecordService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private TokenService tokenService;


    /**
     * 查询影视订单,音乐订单,在线教育订单,商品券订单
     */
    @GetMapping(value = "/queryWiKeYunOrder")
    @ResponseBody
    public AjaxResult queryWiKeYunOrder(HttpServletRequest request, @RequestParam(required = false) Map params) {
        LoginAppUser loginAppUser = tokenService.getLoginAppUser(request);
        SysUser appUser = loginAppUser.getAppUser();
        Long userId = appUser.getUserId();
        params.put("userId", userId);
        Integer productType = TypeUtils.castToInt(params.get("productType"));
        params.put("busType", AppConstant.ORDERNUM_PREFIX_THIRD);
        //已支付
        params.put("status", "20");
        if (StringUtils.isNull(productType)) {
            return AjaxResult.error("缺少订单类型");
        }
        List<Map> records = appPayRecordService.queryThirdColumnOrderList(params);
        String ossBaseUrl = sysConfigService.selectConfigByKey("sys.oss.file.header");
        for (Map map : records) {
            String thirdColumnIcon = (String) map.get("thirdColumnIcon");
            if (StringUtils.isNotEmpty(thirdColumnIcon)) {
                map.put("thirdColumnIcon", ossBaseUrl + thirdColumnIcon);
            }
            String productImg = (String) map.get("productImg");
            if (StringUtils.isNotEmpty(productImg)) {
                map.put("productImg", ossBaseUrl + productImg);
            }
        }
        return AjaxResult.success("获取成功", records);
    }

}
